package com.example.hobby;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.fresh.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Fragment_hobby_one extends Fragment {

    List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>(); //存储数据的数组列表
    int[] image_expense = new int[]{R.drawable.hobby_img3,R.drawable.hobby_img4,R.drawable.hobby_img5,R.drawable.hobby_img6,R.drawable.hobby_img7,R.drawable.hobby_img8,R.drawable.hobby_img9,R.drawable.hobby_img10 }; //存储图片
    int[] image_star = new int[]{R.drawable.hobby_star1,R.drawable.hobby_star2,R.drawable.hobby_star3,R.drawable.hobby_star4,R.drawable.hobby_star5};
    public Fragment_hobby_one() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_hobby_one, container, false);

        String[] expense_category = new String[] {"面包", "火腿肠","鱼罐头","巧克力","空气清新剂","沐浴露","牙膏","香水","牛奶","芬达饮料"};
        for (int i = 0; i < image_expense.length; i++)
        {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image_expense", image_expense[i]);
            map.put("expense_category", expense_category[i]);
            if(i<1)
                map.put("expense_money", image_star[4]);
            else if(i<3)
                map.put("expense_money", image_star[3]);
            else if(i<5)
                map.put("expense_money", image_star[2]);
            else if(i<7)
                map.put("expense_money", image_star[1]);
            else
                map.put("expense_money", image_star[0]);
            listitem.add(map);
        }
        //getData(); //query data from a database

        SimpleAdapter adapter;
        adapter = new SimpleAdapter(getActivity()
                , listitem
                , R.layout.fragment_hobby_one_item
                , new String[]{"expense_category", "expense_money", "image_expense"}
                , new int[]{R.id.tv_expense_category, R.id.tv_expense_money, R.id.image_expense});

        ListView listView = (ListView) v.findViewById(R.id.lv_expense);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {//设置监听器
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map = (Map<String, Object>) parent.getItemAtPosition(position);
                //在点击某笔明细的时候，Tip出明细内容
                Toast.makeText(getActivity(), map.get("expense_category").toString(), Toast.LENGTH_LONG).show();
            }
        });

        return v;
    }

}